﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskManager
{
    public enum TabMode { ALL, AssignedTo, MailTo, OpenedBy, Daily };   // 记录TabSelect控件的选择类型

    public partial class FloatMenu : Form
    {
        int MinWidth = 365, MinHeight = 250;

        public FloatMenu()
        {
            InitializeComponent();
        }

        //public static TaskManage taskManage;       // 任务管理界面

        private void floatMenu_Load(object sender, EventArgs e)
        {
            FormTool.RestoreFormInfo(this);         // 恢复界面信息
            if (this.Width < MinWidth) this.Width = MinWidth;
            if (this.Height < MinHeight) this.Height = MinHeight;

            // 设置tabSelect1显示颜色和背景色
            tabSelect1.ShowColor = Color.FromArgb(202, 81, 0);
            tabSelect1.BackgroundImage = TaskManager.Properties.Resources.背景色_底部;

            // 载入界面后，进行一次界面显示信息的更新
            loginForm.Instance.set_GetWebData_Interval(10);
        }

        # region 浮窗界面控制处理逻辑

        // 控制当前界面的移动
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if(!TopMost) FormTool.MouseDown_MoveForm(this);
        }

        private void 首页_Click(object sender, EventArgs e)
        {
            HttpTool.openUrl("http://" + loginForm.serverIP + "/my/");
        }

        private void 考核_Click(object sender, EventArgs e)
        {
            HttpTool.openUrl("http://" + loginForm.serverIP + "/my-performance.html");
        }

        private void 提醒_Click(object sender, EventArgs e)
        {
            if (ClockForm.RecordForm != null) ClockForm.RecordForm.Close();
            else
            {
                List<Data_Remind> remind = loginForm.Instance.getRecord();    // 获取提醒信息记录
                if (remind.Count > 0) ClockForm.getRecordForm(remind).Show();
                else MessageBox.Show("未曾收到任何提示信息！");
            }
        }

        private void 日记_Click(object sender, EventArgs e)
        {
            HttpTool.openUrl("http://" + loginForm.serverIP + "/my-daily.html");
        }

        private void 考勤_Click(object sender, EventArgs e)
        {
            HttpTool.openUrl("http://" + loginForm.serverIP + "/my-attendance.html");
        }

        private void 导航_Click(object sender, EventArgs e)
        {
            HttpTool.openUrl_Process(null, "http://hijoyactive.joymeng.com/nav/");
        }

        // 任务管理
        private void toolStrip_TaskManage_Click(object sender, EventArgs e)
        {
            //if (taskManage == null)
            //{
            //    taskManage = new TaskManage();
            //    taskManage.Show();
            //}
            //else
            //{
            //    taskManage.Close();
            //    taskManage = null;
            //}
        }

        // 打开新增日志界面
        private void newLog_Click()
        {
            String curDate = DateTime.Today.ToString("yyyyMMdd");
            HttpTool.openUrl("http://" + loginForm.serverIP + "/daily-batchCreate-" + curDate + ".html");
        }

        // 打开周报界面
        private void weekLog_Click()
        {
            HttpTool.openUrl("http://" + loginForm.serverIP + "/index.php?m=my&f=weekly");
        }

        private void ToolStripMenuItem_新建任务_Click(object sender, EventArgs e)
        {
            HttpTool.openUrl("http://" + loginForm.serverIP + "/project-task.html");
        }

        private void 关闭程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            loginForm.Instance.button_exit_Click(null, null);
        }

        # endregion



        # region 新增任务管理界面处理逻辑

        private void 刷新_Click(object sender, EventArgs e)
        {
            //int b = 0;
            //int a = 1 / b;
            loginForm.webDataChange = true;
            loginForm.UpdateData();
        }

        // 更新当前任务管理列表中显示的信息
        public void updateShowing()
        {
            taskSelect_List.clear();

            TabMode Mode = (TabMode)tabSelect1.SeletedIndex;    // 获取显示模式

            taskSelect_List.addInfo(loginForm.TaskData, Mode);
            taskSelect_List.addInfo(loginForm.DemandData, Mode);
            taskSelect_List.addInfo(loginForm.TesttaskData, Mode);
            taskSelect_List.addInfo(loginForm.BugData, Mode);
            taskSelect_List.addInfo(loginForm.DailyData, Mode);

            taskSelect_List.updateSize();
        }


        private void 图钉_Click(object sender, EventArgs e)
        {
            TopMost = !TopMost;
            图钉_MouseEnter(null, null);
        }

        private void 关闭_Click(object sender, EventArgs e)
        {
            this.Close();
            loginForm.floatMenu = null;
        }

        // 新建日志界面
        public static DailyForm dailyForm;      
        private void 新建日志_Click(object sender, EventArgs e)
        {
            //taskSelect_List.addNewIteam();

            // 显示新增日志界面
            if (dailyForm == null)
            {
                dailyForm = new DailyForm();
                dailyForm.Show();

                FormTool.AutoLocation_OnSide(this, dailyForm);
            }
            else if (dailyForm.WindowState != FormWindowState.Normal)
            {
                dailyForm.WindowState = FormWindowState.Normal;
                dailyForm.Activate();
            }
        }


        // 双击控件时响应逻辑
        private void taskSelect_List_DoubleClick(object sender, EventArgs e)
        {
            checkIteam iteam = (checkIteam)sender;

            if (iteam != null)
            {
                bool inForm = ((Control.ModifierKeys & Keys.Control) == Keys.Control); // CTRL is pressed
                openDetail(iteam.IteamType, iteam.Data, inForm);
            }
        }

        // 修改任务check状态时
        private void taskSelect_List_CheckedChanged(object sender, EventArgs e, checkIteam iteam)
        {
            openDetail(iteam.IteamType, iteam.Data);
        }

        // 根据展示的任务类型信息跳转对应的网页
        public static void openDetail(TYPE type, string id, bool inForm)
        {
            if (!inForm) openDetail(type, id);
            else openDetailInForm(type, id);
        }

        // 显示临时提示信息
        public static void openTipDetail(string tittle, string content, string sender, string id, string Mode)
        {
            MessageForm.Show(tittle, content, sender, id, Mode);
        }

        // 打开详情页
        public static void openDetail(TYPE type, string id)
        {
            if (type == TYPE.Task) HttpTool.openUrl("http://" + loginForm.serverIP + "/task-view-" + id + ".html");
            else if (type == TYPE.Demand) HttpTool.openUrl("http://" + loginForm.serverIP + "/story-view-" + id + ".html");
            else if (type == TYPE.Bug) HttpTool.openUrl("http://" + loginForm.serverIP + "/bug-view-" + id + ".html");
            else if (type == TYPE.Testtask) HttpTool.openUrl("http://" + loginForm.serverIP + "/testtask-view-" + id + ".html");
            else if (type == TYPE.Daily) showDailyEdit(id);
            else if (type == TYPE.Food) HttpTool.openUrl("http://" + loginForm.serverIP + "/my-foodorder.htmll");
            else if (type == TYPE.Remind && loginForm.RemindDic.Keys.Contains(id)) MessageForm.ShowRemind(loginForm.RemindDic[id]);  // 显示提示信息弹窗
        }

        // 在Form中显示信息
        public static void openDetailInForm(TYPE type, string id)
        {
            if (type == TYPE.Task) MessageForm.ShowTask(loginForm.TaskDic[id]);
            else if (type == TYPE.Demand) MessageForm.ShowDemand(loginForm.DemandDic[id]);
            else if (type == TYPE.Bug) MessageForm.ShowBug(loginForm.BugDic[id]);
            else if (type == TYPE.Testtask) MessageForm.ShowTesttask(loginForm.TesttaskDic[id]);
            else if (type == TYPE.Daily) showDailyEdit(id);
            else if (type == TYPE.Food) HttpTool.openUrl("http://" + loginForm.serverIP + "/my-foodorder.htmll");
            else if (type == TYPE.Remind && loginForm.RemindDic.Keys.Contains(id)) MessageForm.ShowRemind(loginForm.RemindDic[id]);
        }

        // 显示日志信息编辑界面
        private static void showDailyEdit(string key)
        {
            if (loginForm.DailyDic.Keys.Contains(key))
            {
                DailyForm form = DailyForm.Modify(loginForm.DailyDic[key], true);

                if(loginForm.floatMenu != null) 
                    FormTool.AutoLocation_OnSide(loginForm.floatMenu, form);
            }
        }

        // 选择显示模式，显示对应的列表信息
        private void tabSelect1_Click_This(object sender, EventArgs e)
        {
            //int mode = tabSelect1.SeletedIndex;     // 选中模式
            //MessageBox.Show("选中模式：" + mode);

            updateShowing();
        }

        # endregion

        #region 按钮显示图标控制逻辑

        private void 首页_MouseEnter(object sender, EventArgs e)
        {
            首页.Image = Properties.Resources.主页B;
        }

        private void 首页_MouseLeave(object sender, EventArgs e)
        {
            首页.Image = Properties.Resources.主页A;
        }

        private void 考核_MouseEnter(object sender, EventArgs e)
        {
            考核.Image = Properties.Resources.考核B;
        }

        private void 考核_MouseLeave(object sender, EventArgs e)
        {
            考核.Image = Properties.Resources.考核A;
        }

        private void 提醒_MouseEnter(object sender, EventArgs e)
        {
            提醒.Image = Properties.Resources.提醒B;
        }

        private void 提醒_MouseLeave(object sender, EventArgs e)
        {
            提醒.Image = Properties.Resources.提醒A;
        }

        private void 日记_MouseEnter(object sender, EventArgs e)
        {
            日记.Image = Properties.Resources.日记B;
        }

        private void 日记_MouseLeave(object sender, EventArgs e)
        {
            日记.Image = Properties.Resources.日记A;
        }

        private void 考勤_MouseEnter(object sender, EventArgs e)
        {
            考勤.Image = Properties.Resources.考勤B;
        }

        private void 考勤_MouseLeave(object sender, EventArgs e)
        {
            考勤.Image = Properties.Resources.考勤A;
        }

        private void 导航_MouseEnter(object sender, EventArgs e)
        {
            导航.Image = Properties.Resources.导航B;
        }

        private void 导航_MouseLeave(object sender, EventArgs e)
        {
            导航.Image = Properties.Resources.导航A;
        }

        private void 新建_MouseEnter(object sender, EventArgs e)
        {
            新建日志.Image = Properties.Resources.新建B;
        }

        private void 新建_MouseLeave(object sender, EventArgs e)
        {
            新建日志.Image = Properties.Resources.新建A;
        }

        private void 刷新_MouseEnter(object sender, EventArgs e)
        {
            刷新.Image = Properties.Resources.刷新B;
        }

        private void 刷新_MouseLeave(object sender, EventArgs e)
        {
            刷新.Image = Properties.Resources.刷新A;
        }

        private void 图钉_MouseEnter(object sender, EventArgs e)
        {
            图钉.Image = TopMost ? Properties.Resources.图钉D : Properties.Resources.图钉B;
        }

        private void 图钉_MouseLeave(object sender, EventArgs e)
        {
            图钉.Image = TopMost ? Properties.Resources.图钉C : Properties.Resources.图钉A;
        }

        private void 关闭_MouseEnter(object sender, EventArgs e)
        {
            关闭.Image = Properties.Resources.关闭B;
        }

        private void 关闭_MouseLeave(object sender, EventArgs e)
        {
            关闭.Image = Properties.Resources.关闭A;
        }
        # endregion


        private void 向下缩放_MouseDown(object sender, MouseEventArgs e)
        {
            timer_delayRefresh.Enabled = false;

            FormTool.formResizeMode = 0xF006;
            FormTool.MouseDown_ResizeForm(this);

            timer_delayRefresh.Enabled = true;
        }

        private void 右下缩放_MouseDown(object sender, MouseEventArgs e)
        {
            timer_delayRefresh.Enabled = false;

            FormTool.formResizeMode = 0xF008;
            if (this.Width >= MinWidth)
                FormTool.MouseDown_ResizeForm(this);
            else this.Width = MinWidth;

            timer_delayRefresh.Enabled = true;
        }

        // 界面尺寸变动时，自动更新控件尺寸
        private void panel_content_SizeChanged(object sender, EventArgs e)
        {
            //taskSelect_List.updateSize();
            transPictureBox1.Left = (this.Width - transPictureBox1.Width) / 2;
            transPictureBox1.Top = this.Height - 55;

            //transPictureBox1.Invalidate();  // 添加任务按钮重绘
            //taskSelect_List.Invalidate();   // 自定义控件重绘

            向下缩放.Left = (this.Width - 向下缩放.Width) / 2;
        }

        private void FloatMenu_FormClosing(object sender, FormClosingEventArgs e)
        {
            FormTool.saveFormInfo(this);
        }

        private void FloatMenu_FormClosed(object sender, FormClosedEventArgs e)
        {
            loginForm.floatMenu = null;
        }

        // 在任务管理界面宽度调整后，自动进行一次界面刷新
        private void timer_delayRefresh_Tick(object sender, EventArgs e)
        {
            timer_delayRefresh.Enabled = false;
            taskSelect_List.updateSize();
            //updateShowing();
            //刷新_Click(null, null);
        }

    }
}
